package com.ukao.steward.ui.valetRunners.takeLetter;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.ukao.steward.R;
import com.ukao.steward.adapter.NotTakeLetterAdapter;
import com.ukao.steward.adapter.UnpaidLetterAdapter;
import com.ukao.steward.adapter.YetLetterAdapter;
import com.ukao.steward.base.IAdapter;
import com.ukao.steward.base.IPage;
import com.ukao.steward.base.MvpFragment;
import com.ukao.steward.base.PageWrapper;
import com.ukao.steward.bean.WaitingOrderBean;
import com.ukao.steward.dialog.TransNoDialogFragment;
import com.ukao.steward.dialog.UnTakeDialogFragment;
import com.ukao.steward.listener.DialogSubmitListener;
import com.ukao.steward.listener.OnItemChildClickListener;
import com.ukao.steward.pesenter.valetRunners.TakeLetterPesenter;
import com.ukao.steward.ui.valetRunners.waitingOrder.OrderdetailFragment;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.util.DecimalUtil;
import com.ukao.steward.util.T;
import com.ukao.steward.view.valetRunners.TakeLetterView;
import com.ukao.steward.widget.MyLRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import static com.ukao.steward.R.id.price_btn;
import static com.ukao.steward.R.id.takeName;
import static com.ukao.steward.R.id.userRemark;

/**
 * 取件
 */
public class TakeLetterFragment extends MvpFragment<TakeLetterPesenter>  implements TakeLetterView {


    @BindView(R.id.lrecycler_view)
    MyLRecyclerView lrecyclerView;
    Unbinder unbinder;
    private int mColumnCount;
    private NotTakeLetterAdapter mNotTakeLetterAdapter;
    private LRecyclerViewAdapter mLRecyclerViewAdapter;
    private List<WaitingOrderBean.ListBean> mData;
    private PageWrapper mPageWrapper;
    private UnpaidLetterAdapter mUnpaidLetterAdapter;
    private YetLetterAdapter mYetLetterAdapter;
    public WaitingOrderBean.ListBean mTakeItem;
    private View view;
    private TakeLetterTabFragment mTakeLetterTabFragment;
    private int total;
    private View mEmptyView;


    public static TakeLetterFragment newInstance(int columnCount) {
        TakeLetterFragment fragment = new TakeLetterFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, columnCount);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            mColumnCount = getArguments().getInt(ARG_PARAM1);
        }
    }

    @Override
    protected TakeLetterPesenter createPresenter() {
        return new TakeLetterPesenter(this);
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();

        lrecyclerView.forceToRefresh();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
         view= inflater.inflate(R.layout.fragment_enter_factorys, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        initListener();
        return view;
    }



    @Override
    protected void initView() {

        mData= new ArrayList<>();
         mEmptyView= view.findViewById(R.id.empty_view);
        //lrecyclerView.setEmptyView(mEmptyView);//设置在setAdapter之前才能生效
        initLinearRecyclerView(lrecyclerView);
        initAdapter();
        IPage page = new IPage() {
            @Override
            public void load(int geIndex, int pageSize) {
                mvpPresenter.requestReceiving(geIndex,pageSize,mColumnCount);

            }
        };
        mPageWrapper= new PageWrapper<>(mIAdapter, page);

    }

    private void initAdapter() {
        switch (mColumnCount) {
            case 1:/*未取*/
                mNotTakeLetterAdapter =new NotTakeLetterAdapter(_mActivity, mData);
                mLRecyclerViewAdapter= new LRecyclerViewAdapter(mNotTakeLetterAdapter);
                break;
            case 2:/*未交*/
                mUnpaidLetterAdapter =new UnpaidLetterAdapter(_mActivity, mData);
                mLRecyclerViewAdapter= new LRecyclerViewAdapter(mUnpaidLetterAdapter);
                break;
            case 3:/*已交*/
                mYetLetterAdapter =new YetLetterAdapter(_mActivity, mData);
                mLRecyclerViewAdapter= new LRecyclerViewAdapter(mYetLetterAdapter);
                break;
        }



        lrecyclerView.setAdapter(mLRecyclerViewAdapter);
    }


    IAdapter mIAdapter = new IAdapter() {
        @Override
        public void setDataSource(List data, boolean isNextLoad) {
            mEmptyView.setVisibility(CheckUtils.isEmpty(data)?View.VISIBLE:View.GONE);
            cleanData();
            setNewData(data);
            mLRecyclerViewAdapter.notifyDataSetChanged();
            lrecyclerView.setLoadMoreEnabled(isNextLoad);


        }

        @Override
        public void loadMoreFinish(List data, boolean isNextLoad) {
            setLoadMoreData(data);
            mLRecyclerViewAdapter.notifyDataSetChanged();
            if (!isNextLoad) lrecyclerView.setNoMore(!isNextLoad);
        }
    };




    @Override
    protected void initListener() {
        super.initListener();
        lrecyclerView.setOnRefreshListener(mOnRefreshListener);
        lrecyclerView.setOnLoadMoreListener(mOnLoadMoreListener);
        setItemChildListener();
        mLRecyclerViewAdapter.setOnItemClickListener(mOnItemClickListener);

    }



    private OnItemClickListener mOnItemClickListener=new OnItemClickListener() {
        public String takeName;
        public String takePhone;

        @Override
        public void onItemClick(View view, int position) {
        String   orderid="";
            switch (mColumnCount) {
                case 1:/*未取*/

                    orderid= mNotTakeLetterAdapter.getCount(position).getId();
                    takePhone=mNotTakeLetterAdapter.getCount(position).getTakePhone();
                    takeName=mNotTakeLetterAdapter.getCount(position).getTakeName();
                    break;
                case 2:/*未交*/
                    orderid= mUnpaidLetterAdapter.getCount(position).getId();
                    takePhone=mUnpaidLetterAdapter.getCount(position).getTakePhone();
                    takeName=mUnpaidLetterAdapter.getCount(position).getTakeName();
                    break;
                case 3:/*已交*/
                    orderid= mYetLetterAdapter.getCount(position).getId();
                    break;
            }
            if(getParentFragment() instanceof TakeLetterTabFragment) {
                mTakeLetterTabFragment = ((TakeLetterTabFragment) getParentFragment());
                if (mColumnCount==3?false:true){
                    OrderdetailFragment  mOrderdetailFragment=OrderdetailFragment.createBuilder()
                            .orderId(orderid)
                            .isValuation(true)
                            .takeName(takeName)
                            .takephone(takePhone)
                            .build();
                    mTakeLetterTabFragment.start(mOrderdetailFragment);
                }else{
                    OrderdetailFragment  mOrderdetailFragment=OrderdetailFragment.createBuilder()
                            .orderId(orderid)
                            .isValuation(false)
                            .build();
                    mTakeLetterTabFragment.start(mOrderdetailFragment);
                }


            }
        }
    };


    private OnRefreshListener mOnRefreshListener = new OnRefreshListener() {
        @Override
        public void onRefresh() {
            cleanData();
            mLRecyclerViewAdapter.notifyDataSetChanged();
            mPageWrapper.loadPage(true);
        }
    };
    private OnLoadMoreListener mOnLoadMoreListener=new OnLoadMoreListener() {
        @Override
        public void onLoadMore() {
            mPageWrapper.loadPage(false);
        }
    };


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    public void receiveNoTakeSucceed(WaitingOrderBean mWaitingOrderBean ) {
        if(getParentFragment() instanceof TakeLetterTabFragment) {
            mTakeLetterTabFragment=((TakeLetterTabFragment) getParentFragment());
            total=mWaitingOrderBean.getTotal();
            switch (mColumnCount) {
                case 1:
                    mTakeLetterTabFragment.updataTakeNotTake(mWaitingOrderBean.getTotal());
                    break;
                case 2:
                    mTakeLetterTabFragment.updataTakeNotDeal(mWaitingOrderBean.getTotal());
                    break;
            }
        }
        mPageWrapper.addDataSource(mWaitingOrderBean.getList(), mWaitingOrderBean.getPages());
    }
    @Override
    public void modifyPrice(String price ,String msg) {
        mTakeItem.setModifyPrice(Integer.parseInt(price));
        mUnpaidLetterAdapter.notifyDataSetChanged();
        T.show(msg);
    }
    @Override
    public void loadfinish() {
        lrecyclerView.refreshFinish();

    }

    @Override
    public void receivingSucceed(WaitingOrderBean.ListBean mListBean, String msg) {
       // T.show(msg);
        switch (mColumnCount) {
            case 1:/*未取*/
                List<WaitingOrderBean.ListBean>     mData=mNotTakeLetterAdapter.getDataList();
                for (int i = 0; i <mData.size(); i++) {
                    WaitingOrderBean.ListBean itemBean=mData.get(i);
                    if (mListBean.getId()==itemBean.getId()){
                        if (total!=0){
                            total=total-1;
                        }
                        if (!CheckUtils.isNull(mTakeLetterTabFragment)){
                            mTakeLetterTabFragment.updataTakeNotTake(total);
                        }
                        mNotTakeLetterAdapter.remove(i);
                        mLRecyclerViewAdapter.notifyDataSetChanged();
                    }
                }


                break;

        }


    }



    public  void   cleanData( ){
        switch (mColumnCount) {
            case 1:/*未取*/
                mNotTakeLetterAdapter.getDataList().clear();
                break;
            case 2:/*未交*/
                mUnpaidLetterAdapter.getDataList().clear();
                break;
            case 3:/*已交*/
                mYetLetterAdapter.getDataList().clear();

                break;
        }
    }

    public  void   setNewData(List data){
        switch (mColumnCount) {
            case 1:/*未取*/
                mNotTakeLetterAdapter.setDataList(data);
                break;
            case 2:/*未交*/
                mUnpaidLetterAdapter.setDataList(data);
                break;
            case 3:/*已交*/
                mYetLetterAdapter.setDataList(data);

                break;
        }
    }


    public  void   setItemChildListener( ){
        switch (mColumnCount) {
            case 1:/*未取*/
                mNotTakeLetterAdapter.setOnItemChildClickListener(mOnItemChildClick);
                break;
            case 2:/*未交*/
                mUnpaidLetterAdapter.setOnItemChildClickListener(mOnItemChildClick);
                break;
            case 3:/*已交*/
                mYetLetterAdapter.setOnItemChildClickListener(mOnItemChildClick);

                break;
        }
    }
private OnItemChildClickListener mOnItemChildClick=new OnItemChildClickListener() {


    @Override
    public void onChildItemClick(int id, int pos, Object o) {
        mTakeItem = (WaitingOrderBean.ListBean) o;
        switch (id) {
           /* case R.id.prices_btn:*//*修改价格*//*

                UnTakeDialogFragment mChangeDialogFragment = UnTakeDialogFragment.newInstance(2, R.id.prices_btn);
                mChangeDialogFragment.setOnSubmitClickListener(mDialogSubmitListener);
                mChangeDialogFragment.show(getChildFragmentManager(),UnTakeDialogFragment.class.getName());
                break;*/
            case R.id.record_btn:/*录单号*/
                TransNoDialogFragment.createBuilder(getChildFragmentManager())
                        .orderId(mTakeItem.getId())
                        .mListener(mDialogSubmitListener)
                        .show();



                break;
            case  R.id.price_btn:  /*计价*/
                String   orderid="";
                switch (mColumnCount) {
                    case 1:/*未取*/

                        orderid=mTakeItem.getId();

                        break;
                    case 2:/*未交*/
                        orderid= mTakeItem.getId();

                        break;

                }
                if(getParentFragment() instanceof TakeLetterTabFragment) {
                    mTakeLetterTabFragment = ((TakeLetterTabFragment) getParentFragment());
                    OrderdetailFragment  mOrderdetailFragment=OrderdetailFragment.createBuilder()
                            .orderId(orderid)
                            .isValuation(true)
                            .takeName(mTakeItem.getTakeName())
                            .takephone(mTakeItem.getTakePhone())
                            .build();
                    mTakeLetterTabFragment.start(mOrderdetailFragment);

                }
                break;
        }

    }
};




private DialogSubmitListener  mDialogSubmitListener=new DialogSubmitListener() {
    @Override
    public void onSubmitClick(int parent, Object message) {
    //  String  context=(String)message;
        receivingSucceed(mTakeItem,"");
//        switch (parent) {
//            case R.id.record_btn:/*未取单号*/
//
//                mvpPresenter.addtransNo(mTakeItem,context);
//                break;
////            case R.id.prices_btn: /*未交计价*/
////
////                mvpPresenter.modifyPrice(mTakeItem,DecimalUtil.multiply(context));
////                break;
//
//
//
//
//        }
    }
};



    public  void   setLoadMoreData(List data){
        switch (mColumnCount) {
            case 1:/*未取*/
                mNotTakeLetterAdapter.addAll(data);
                break;
            case 2:/*未交*/
                mUnpaidLetterAdapter.addAll(data);
                break;
            case 3:/*已交*/
                mYetLetterAdapter.addAll(data);
                break;
        }
    }





}
